package com.pmcc.cj.service;

import com.pmcc.cj.entity.CjActivationSale;
import com.pmcc.cj.repository.CjActivationSaleRepository;
import com.pmcc.core.entity.Employee;
import com.pmcc.core.repository.EmployeeRepository;
import com.pmcc.utils.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by tomas on 2020/10/12.
 */
@Service
@Transactional
public class CjActivationSaleService {
    @Autowired
    EmployeeRepository employeeRepository;
    @Autowired
    CjActivationSaleRepository cjActivationSaleRepository;

    public ResultBean queryByPage(AntdPage antdPage, CjActivationSale model){
        ResultBean resultBean = new ResultBean(ResultBean.SUCCESS);
        try {
            Page page = cjActivationSaleRepository.findAll(this.getSpecifition(model), antdPage.getPageRequest());
            resultBean.setPage(antdPage.getDefaultCurrent());
            resultBean.setTotal(page.getTotalElements());
            resultBean.setPageSize(antdPage.getPageSize());
            resultBean.setRows(page.getContent());
        }catch (Exception e){
            resultBean.setResultCode(ResultBean.FAIL);
        }
        return resultBean;
    }
    public Specification<CjActivationSale> getSpecifition(CjActivationSale model) {
        return new Specification<CjActivationSale>() {
            @Override
            public Predicate toPredicate(Root<CjActivationSale> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
                List<Predicate> list = new ArrayList<>();
                if(StringUtil.isNotEmpty(model.getEmployeeId())){
                    list.add(builder.equal(root.get("employeeId").as(String.class),  model.getEmployeeId()));
                }
                if(model.getSearchTime() != null && model.getSearchTime().length>1){
                    list.add(builder.between(root.get("createDate").as(Date.class), DateUtil.StringToDate(model.getSearchTime()[0], DateUtil.YYYY_MM_DD_HH_MM_SS), DateUtil.StringToDate(model.getSearchTime()[1], DateUtil.YYYY_MM_DD_HH_MM_SS)));
                }
                if(model.getCodeType() != null){
                    list.add(builder.equal(root.get("codeType").as(Integer.class),model.getCodeType()));
                }
                query.orderBy(builder.desc(root.get("createDate").as(Date.class)));
                Predicate[] p = new Predicate[list.size()];
                return builder.and(list.toArray(p));
            }
        };
    }
    public ResultBean addEmployeeCode(CjActivationSale model){
        ResultBean resultBean = new ResultBean(ResultBean.SUCCESS);
        try {
            if(model.getCodeType() == 0 || model.getCodeType() == 2){//县级代理或者零售商
                CjActivationSale employeeCode = cjActivationSaleRepository.findByActivationCodeAndDelFlag(model.getActivationCode(),0);
                if(employeeCode != null){
                    resultBean.setResultCode(ResultBean.FAIL);
                    resultBean.setResultDesc("激活码已使用");
                    return resultBean;
                }
                model.setCreateDate(new Date());
                model.setDelFlag(0);
                model.setUseFlag(0);
//                model.setCodeType(0);
                cjActivationSaleRepository.saveAndFlush(model);
            }else if(model.getCodeType() == 1){//种地大户
                for(int i=0;i<model.getCodeNumber();i++){
                    CjActivationSale activationSale = new CjActivationSale();
                    activationSale.setEmployeeId(model.getEmployeeId());
                    activationSale.setEmployeeName(model.getEmployeeName());
                    activationSale.setActivationCode("Farm_"+ ActivationCodeUtil.getActivationCode());
                    activationSale.setCreateDate(new Date());
                    activationSale.setCodeType(1);
                    activationSale.setUseFlag(0);
                    activationSale.setDelFlag(0);
                    cjActivationSaleRepository.saveAndFlush(activationSale);
                }
            }
        }catch (Exception e){
            resultBean.setResultCode(ResultBean.FAIL);
        }
        return resultBean;
    }
    public ResultBean deleteEmployeeCode(String id){
        ResultBean resultBean  = new ResultBean(ResultBean.SUCCESS);
        try {
            CjActivationSale employeeCode = cjActivationSaleRepository.findOne(id);
            if(employeeCode != null && employeeCode.getUseFlag()==1){
                resultBean.setResultCode(ResultBean.FAIL);
                resultBean.setResultDesc("激活码已使用,不能删除");
                return resultBean;
            }
            cjActivationSaleRepository.delete(id);
        }catch (Exception e){
            resultBean.setResultCode(ResultBean.FAIL);
        }
        return resultBean;
    }
    public ResultBean findEmployeeCode(){
        ResultBean resultBean = new ResultBean(ResultBean.SUCCESS);
        try {
            List<List<String>> list =new ArrayList<>();
            List<Employee> ll=employeeRepository.findBySaleMan(true);
            if(ll != null && ll.size()>0){
                for(Employee employee: ll){
                    List<String> ls = new ArrayList<>();
                    ls.add(employee.getId());
                    ls.add(employee.getName());
                    list.add(ls);
                }
            }
            resultBean.setRows(list);
        }catch (Exception e){
            resultBean.setResultCode(ResultBean.FAIL);
        }
        return resultBean;
    }

}
